{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 2.12 \u5ba1\u67e5\u6e05\u7406\u6587\u672c\u5b57\u7b26\u4e32\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u4e9b\u65e0\u804a\u7684\u5e7c\u7a1a\u9ed1\u5ba2\u5728\u4f60\u7684\u7f51\u7ad9\u9875\u9762\u8868\u5355\u4e2d\u8f93\u5165\u6587\u672c\u201dp\u00fdt\u0125\u00f6\u00f1\u201d\uff0c\u7136\u540e\u4f60\u60f3\u5c06\u8fd9\u4e9b\u5b57\u7b26\u6e05\u7406\u6389\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6587\u672c\u6e05\u7406\u95ee\u9898\u4f1a\u6d89\u53ca\u5230\u5305\u62ec\u6587\u672c\u89e3\u6790\u4e0e\u6570\u636e\u5904\u7406\u7b49\u4e00\u7cfb\u5217\u95ee\u9898\u3002\n\u5728\u975e\u5e38\u7b80\u5355\u7684\u60c5\u5f62\u4e0b\uff0c\u4f60\u53ef\u80fd\u4f1a\u9009\u62e9\u4f7f\u7528\u5b57\u7b26\u4e32\u51fd\u6570(\u6bd4\u5982 str.upper() \u548c str.lower() )\u5c06\u6587\u672c\u8f6c\u4e3a\u6807\u51c6\u683c\u5f0f\u3002\n\u4f7f\u7528 str.replace() \u6216\u8005 re.sub() \u7684\u7b80\u5355\u66ff\u6362\u64cd\u4f5c\u80fd\u5220\u9664\u6216\u8005\u6539\u53d8\u6307\u5b9a\u7684\u5b57\u7b26\u5e8f\u5217\u3002\n\u4f60\u540c\u6837\u8fd8\u53ef\u4ee5\u4f7f\u75282.9\u5c0f\u8282\u7684 unicodedata.normalize() \u51fd\u6570\u5c06unicode\u6587\u672c\u6807\u51c6\u5316\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7136\u540e\uff0c\u6709\u65f6\u5019\u4f60\u53ef\u80fd\u8fd8\u60f3\u5728\u6e05\u7406\u64cd\u4f5c\u4e0a\u66f4\u8fdb\u4e00\u6b65\u3002\u6bd4\u5982\uff0c\u4f60\u53ef\u80fd\u60f3\u6d88\u9664\u6574\u4e2a\u533a\u95f4\u4e0a\u7684\u5b57\u7b26\u6216\u8005\u53bb\u9664\u53d8\u97f3\u7b26\u3002\n\u4e3a\u4e86\u8fd9\u6837\u505a\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u7ecf\u5e38\u4f1a\u88ab\u5ffd\u89c6\u7684 str.translate() \u65b9\u6cd5\u3002\n\u4e3a\u4e86\u6f14\u793a\uff0c\u5047\u8bbe\u4f60\u73b0\u5728\u6709\u4e0b\u9762\u8fd9\u4e2a\u51cc\u4e71\u7684\u5b57\u7b26\u4e32\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s = 'p\u00fdt\u0125\u00f6\u00f1\\fis\\tawesome\\r\\n'\ns"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7b2c\u4e00\u6b65\u662f\u6e05\u7406\u7a7a\u767d\u5b57\u7b26\u3002\u4e3a\u4e86\u8fd9\u6837\u505a\uff0c\u5148\u521b\u5efa\u4e00\u4e2a\u5c0f\u7684\u8f6c\u6362\u8868\u683c\u7136\u540e\u4f7f\u7528 translate() \u65b9\u6cd5\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "remap = {\n    ord('\\t') : ' ',\n    ord('\\f') : ' ',\n    ord('\\r') : None # Deleted\n}\na = s.translate(remap)\na"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6b63\u5982\u4f60\u770b\u7684\u90a3\u6837\uff0c\u7a7a\u767d\u5b57\u7b26 \\t \u548c \\f \u5df2\u7ecf\u88ab\u91cd\u65b0\u6620\u5c04\u5230\u4e00\u4e2a\u7a7a\u683c\u3002\u56de\u8f66\u5b57\u7b26r\u76f4\u63a5\u88ab\u5220\u9664\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u53ef\u4ee5\u4ee5\u8fd9\u4e2a\u8868\u683c\u4e3a\u57fa\u7840\u8fdb\u4e00\u6b65\u6784\u5efa\u66f4\u5927\u7684\u8868\u683c\u3002\u6bd4\u5982\uff0c\u8ba9\u6211\u4eec\u5220\u9664\u6240\u6709\u7684\u548c\u97f3\u7b26\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import unicodedata\nimport sys\ncmb_chrs = dict.fromkeys(c for c in range(sys.maxunicode)\n                        if unicodedata.combining(chr(c)))\nb = unicodedata.normalize('NFD', a)\nb"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "b.translate(cmb_chrs)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0a\u9762\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u4f7f\u7528 dict.fromkeys() \u65b9\u6cd5\u6784\u9020\u4e00\u4e2a\u5b57\u5178\uff0c\u6bcf\u4e2aUnicode\u548c\u97f3\u7b26\u4f5c\u4e3a\u952e\uff0c\u5bf9\u5e94\u7684\u503c\u5168\u90e8\u4e3a None \u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7136\u540e\u4f7f\u7528 unicodedata.normalize() \u5c06\u539f\u59cb\u8f93\u5165\u6807\u51c6\u5316\u4e3a\u5206\u89e3\u5f62\u5f0f\u5b57\u7b26\u3002\n\u7136\u540e\u518d\u8c03\u7528 translate \u51fd\u6570\u5220\u9664\u6240\u6709\u91cd\u97f3\u7b26\u3002\n\u540c\u6837\u7684\u6280\u672f\u4e5f\u53ef\u4ee5\u88ab\u7528\u6765\u5220\u9664\u5176\u4ed6\u7c7b\u578b\u7684\u5b57\u7b26(\u6bd4\u5982\u63a7\u5236\u5b57\u7b26\u7b49)\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u4f8b\u5b50\uff0c\u8fd9\u91cc\u6784\u9020\u4e00\u4e2a\u5c06\u6240\u6709Unicode\u6570\u5b57\u5b57\u7b26\u6620\u5c04\u5230\u5bf9\u5e94\u7684ASCII\u5b57\u7b26\u4e0a\u7684\u8868\u683c\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "digitmap = { c: ord('0') + unicodedata.digit(chr(c))\n        for c in range(sys.maxunicode)\n        if unicodedata.category(chr(c)) == 'Nd' }\nlen(digitmap)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Arabic digits\nx = '\\u0661\\u0662\\u0663'\nx.translate(digitmap)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53e6\u4e00\u79cd\u6e05\u7406\u6587\u672c\u7684\u6280\u672f\u6d89\u53ca\u5230I/O\u89e3\u7801\u4e0e\u7f16\u7801\u51fd\u6570\u3002\u8fd9\u91cc\u7684\u601d\u8def\u662f\u5148\u5bf9\u6587\u672c\u505a\u4e00\u4e9b\u521d\u6b65\u7684\u6e05\u7406\uff0c\n\u7136\u540e\u518d\u7ed3\u5408 encode() \u6216\u8005 decode() \u64cd\u4f5c\u6765\u6e05\u9664\u6216\u4fee\u6539\u5b83\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "b = unicodedata.normalize('NFD', a)\nb.encode('ascii', 'ignore').decode('ascii')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u91cc\u7684\u6807\u51c6\u5316\u64cd\u4f5c\u5c06\u539f\u6765\u7684\u6587\u672c\u5206\u89e3\u4e3a\u5355\u72ec\u7684\u548c\u97f3\u7b26\u3002\u63a5\u4e0b\u6765\u7684ASCII\u7f16\u7801/\u89e3\u7801\u53ea\u662f\u7b80\u5355\u7684\u4e00\u4e0b\u5b50\u4e22\u5f03\u6389\u90a3\u4e9b\u5b57\u7b26\u3002\n\u5f53\u7136\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4ec5\u4ec5\u53ea\u5728\u6700\u540e\u7684\u76ee\u6807\u5c31\u662f\u83b7\u53d6\u5230\u6587\u672c\u5bf9\u5e94ACSII\u8868\u793a\u7684\u65f6\u5019\u751f\u6548\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6587\u672c\u5b57\u7b26\u6e05\u7406\u4e00\u4e2a\u6700\u4e3b\u8981\u7684\u95ee\u9898\u5e94\u8be5\u662f\u8fd0\u884c\u7684\u6027\u80fd\u3002\u4e00\u822c\u6765\u8bb2\uff0c\u4ee3\u7801\u8d8a\u7b80\u5355\u8fd0\u884c\u8d8a\u5feb\u3002\n\u5bf9\u4e8e\u7b80\u5355\u7684\u66ff\u6362\u64cd\u4f5c\uff0c str.replace() \u65b9\u6cd5\u901a\u5e38\u662f\u6700\u5feb\u7684\uff0c\u751a\u81f3\u5728\u4f60\u9700\u8981\u591a\u6b21\u8c03\u7528\u7684\u65f6\u5019\u3002\n\u6bd4\u5982\uff0c\u4e3a\u4e86\u6e05\u7406\u7a7a\u767d\u5b57\u7b26\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def clean_spaces(s):\n    s = s.replace('\\r', '')\n    s = s.replace('\\t', ' ')\n    s = s.replace('\\f', ' ')\n    return s"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u53bb\u6d4b\u8bd5\u7684\u8bdd\uff0c\u4f60\u5c31\u4f1a\u53d1\u73b0\u8fd9\u79cd\u65b9\u5f0f\u4f1a\u6bd4\u4f7f\u7528 translate() \u6216\u8005\u6b63\u5219\u8868\u8fbe\u5f0f\u8981\u5feb\u5f88\u591a\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53e6\u4e00\u65b9\u9762\uff0c\u5982\u679c\u4f60\u9700\u8981\u6267\u884c\u4efb\u4f55\u590d\u6742\u5b57\u7b26\u5bf9\u5b57\u7b26\u7684\u91cd\u65b0\u6620\u5c04\u6216\u8005\u5220\u9664\u64cd\u4f5c\u7684\u8bdd\uff0c tanslate() \u65b9\u6cd5\u4f1a\u975e\u5e38\u7684\u5feb\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4ece\u5927\u7684\u65b9\u9762\u6765\u8bb2\uff0c\u5bf9\u4e8e\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u6765\u8bf4\u6027\u80fd\u662f\u4f60\u4e0d\u5f97\u4e0d\u53bb\u81ea\u5df1\u7814\u7a76\u7684\u4e1c\u897f\u3002\n\u4e0d\u5e78\u7684\u662f\uff0c\u6211\u4eec\u4e0d\u53ef\u80fd\u7ed9\u4f60\u5efa\u8bae\u4e00\u4e2a\u7279\u5b9a\u7684\u6280\u672f\uff0c\u4f7f\u5b83\u80fd\u591f\u9002\u5e94\u6240\u6709\u7684\u60c5\u51b5\u3002\n\u56e0\u6b64\u5b9e\u9645\u60c5\u51b5\u4e2d\u9700\u8981\u4f60\u81ea\u5df1\u53bb\u5c1d\u8bd5\u4e0d\u540c\u7684\u65b9\u6cd5\u5e76\u8bc4\u4f30\u5b83\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c3d\u7ba1\u8fd9\u4e00\u8282\u96c6\u4e2d\u8ba8\u8bba\u7684\u662f\u6587\u672c\uff0c\u4f46\u662f\u7c7b\u4f3c\u7684\u6280\u672f\u4e5f\u53ef\u4ee5\u9002\u7528\u4e8e\u5b57\u8282\uff0c\u5305\u62ec\u7b80\u5355\u7684\u66ff\u6362\uff0c\u8f6c\u6362\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}